package com.example.mqtt.controller;

import com.example.mqtt.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/api/task")
public class TaskController {
    private final TaskService taskControlService;

    @Autowired
    public TaskController(TaskService taskControlService) {
        this.taskControlService = taskControlService;
    }

    @GetMapping("/status")
    public ResponseEntity<Boolean> getTaskStatus() {
        return ResponseEntity.ok(taskControlService.isTaskEnabled());
    }

    @PostMapping("/toggle")
    public ResponseEntity<String> toggleTask(@RequestParam boolean enable) {
        taskControlService.setTaskEnabled(enable);
        return ResponseEntity.ok("任务状态已更新: " + (enable ? "启用" : "禁用"));
    }

}
